﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.IsolatedStorage;

namespace QuickWeb
{
    class AppSettings
    {
        IsolatedStorageSettings settings;
        const string selectedSearchEngineKeyName = "SearchEngineSetting";
        const string selectedStartPivotKeyName = "StartPivotSetting";
        const string selectedUpdateFeedsAutomaticallyKeyName = "UpdateFeedsAutomaticallySetting";
        const string selectedLoadFeedsImagesKeyName = "LoadImagesSetting";
        const string selectedShowPreviewMaxLengthKeyName = "ShowPreviewMaxLengthSetting";

        //El motor de busqueda seleccionado
        //0-> Bing
        //1-> Google
        //2-> DuckDuckGo
        const int SearchEngineSettingDefault = 0;
        //El pivot de inicio
        //0->Search
        //1->Read
        //2->Clips
        const int StartPivotSettingDefault = 1;
        //Update feeds on open read tab
        const bool UpdateFeedsAutomaticallySettingDefault = true;
        //S se debe cargar imagenes de los feeds
        //0-> No
        //1-> Siempre
        //1-> Solo en Wifi
        const int LoadFeedsImagesSettingDefault = 1;
        //Limite de caracteres con el que se muestra la descripcion
        //0-> 200
        //1-> 500
        //2-> Sin Limite
        const int ShowPreviewMaxLengthSettingDefault = 0;


        public AppSettings() { 
            try
            {
                // Get the settings for this application.
                settings = IsolatedStorageSettings.ApplicationSettings;
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("Exception while using IsolatedStorageSettings: " + e.ToString());
            }
        }


        public bool AddOrUpdateValue(string Key, Object value)
        {
            bool valueChanged = false;

            // If the key exists
            if (settings.Contains(Key))
            {
                // If the value has changed
                if (settings[Key] != value)
                {
                    // Store the new value
                    settings[Key] = value;
                    valueChanged = true;
                }
            }
            // Otherwise create the key.
            else
            {
                settings.Add(Key, value);
                valueChanged = true;
            }
            return valueChanged;
        }

        public T GetValueOrDefault<T>(string Key, T defaultValue)
        {
            T value;

            // If the key exists, retrieve the value.
            if (settings.Contains(Key))
            {
                value = (T)settings[Key];               
            }
            // Otherwise, use the default value.
            else
            {
                value = defaultValue;
            }
            return value;
        }

        public void SaveSettings()
        {
            settings.Save();
        }

        public int SearchEngine
        {
            get
            {
                return GetValueOrDefault<int>(selectedSearchEngineKeyName, SearchEngineSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(selectedSearchEngineKeyName, value))
                {
                    SaveSettings();
                }
            }
        }

        public int StartPivot
        {
            get
            {
                return GetValueOrDefault<int>(selectedStartPivotKeyName, StartPivotSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(selectedStartPivotKeyName, value))
                {
                    SaveSettings();
                }
            }
        }

        public bool UpdateFeedsAutomatically
        {
            get
            {
                return GetValueOrDefault<bool>(selectedUpdateFeedsAutomaticallyKeyName, UpdateFeedsAutomaticallySettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(selectedUpdateFeedsAutomaticallyKeyName, value))
                {
                    SaveSettings();
                }
            }
        }

        public int LoadFeedsImages
        {
            get
            {
                return GetValueOrDefault<int>(selectedLoadFeedsImagesKeyName, LoadFeedsImagesSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(selectedLoadFeedsImagesKeyName, value))
                {
                    SaveSettings();
                }
            }
        }

        public int ShowPreviewMaxLength
        {
            get
            {
                return GetValueOrDefault<int>(selectedShowPreviewMaxLengthKeyName, ShowPreviewMaxLengthSettingDefault);
            }
            set
            {
                if (AddOrUpdateValue(selectedShowPreviewMaxLengthKeyName, value))
                {
                    SaveSettings();
                }
            }
        }




    }
}
